package com.zhiwen.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhiwen.common.api.Result;
import com.zhiwen.domain.dto.UserDTO;
import com.zhiwen.domain.entity.Answer;
import com.zhiwen.domain.entity.Question;
import com.zhiwen.domain.entity.User;
import com.zhiwen.service.AnswerService;
import com.zhiwen.service.QuestionService;
import com.zhiwen.service.UserService;
import com.zhiwen.domain.vo.AnswerVO;
import com.zhiwen.domain.vo.QuestionVO;
import com.zhiwen.domain.vo.UserVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 用户控制器
 */
@Tag(name = "用户管理", description = "用户信息相关接口")
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;
    private final QuestionService questionService;
    private final AnswerService answerService;

    /**
     * 获取用户信息
     */
    @Operation(summary = "获取用户信息", description = "根据用户ID获取用户详细信息")
    @ApiResponse(responseCode = "200", description = "获取成功",
                content = @Content(schema = @Schema(implementation = UserVO.class)))
    @GetMapping("/{id}")
    public Result<UserVO> getUserInfo(@Parameter(description = "用户ID") @PathVariable Long id) {
        UserVO userVO = userService.getUserInfo(id);
        return Result.success(userVO);
    }

    /**
     * 获取当前登录用户信息
     */
    @Operation(summary = "获取当前登录用户信息", description = "获取当前登录用户的详细信息，需要登录")
    @SecurityRequirement(name = "Authorization")
    @ApiResponse(responseCode = "200", description = "获取成功",
                content = @Content(schema = @Schema(implementation = UserVO.class)))
    @GetMapping("/me")
    @SaCheckLogin
    public Result<UserVO> getCurrentUser() {
        // 获取当前登录用户
        Long userId = StpUtil.getLoginIdAsLong();
        User user = userService.getById(userId);

        // 转换为视图对象
        UserVO userVO = userService.getUserVO(user);

        return Result.success(userVO);
    }

    /**
     * 更新当前登录用户信息
     */
    @Operation(summary = "更新当前登录用户信息", description = "更新当前登录用户的个人资料，需要登录")
    @SecurityRequirement(name = "Authorization")
    @ApiResponse(responseCode = "200", description = "更新成功",
                content = @Content(schema = @Schema(implementation = UserVO.class)))
    @PutMapping("/me")
    @SaCheckLogin
    public Result<UserVO> updateUserInfo(@RequestBody UserDTO userDTO) {
        Long userId = StpUtil.getLoginIdAsLong();
        userDTO.setId(userId);
        UserVO userVO = userService.updateUserInfo(userDTO);
        return Result.success(userVO);
    }

    /**
     * 获取用户提问列表
     */
    @Operation(summary = "获取用户提问列表", description = "分页获取指定用户发布的问题列表")
    @Parameters({
        @Parameter(name = "id", description = "用户ID"),
        @Parameter(name = "current", description = "当前页码", schema = @Schema(defaultValue = "1")),
        @Parameter(name = "size", description = "每页数量", schema = @Schema(defaultValue = "10"))
    })
    @ApiResponse(responseCode = "200", description = "获取成功")
    @GetMapping("/{id}/questions")
    public Result<Page<QuestionVO>> getUserQuestions(
            @PathVariable Long id,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<Question> page = new Page<>(current, size);
        Page<QuestionVO> questionPage = questionService.getUserQuestions(id, page);
        return Result.success(questionPage);
    }

    /**
     * 获取用户回答列表
     */
    @Operation(summary = "获取用户回答列表", description = "分页获取指定用户发布的回答列表")
    @Parameters({
        @Parameter(name = "id", description = "用户ID"),
        @Parameter(name = "current", description = "当前页码", schema = @Schema(defaultValue = "1")),
        @Parameter(name = "size", description = "每页数量", schema = @Schema(defaultValue = "10"))
    })
    @ApiResponse(responseCode = "200", description = "获取成功")
    @GetMapping("/{id}/answers")
    public Result<Page<AnswerVO>> getUserAnswers(
            @PathVariable Long id,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<Answer> page = new Page<>(current, size);
        Page<AnswerVO> answerPage = answerService.getUserAnswers(id, page);
        return Result.success(answerPage);
    }
}
